package com.example.fbi_text.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.fbi_text.Acitivity.CartActivity;
import com.example.fbi_text.R;
import com.example.fbi_text.bean.Product;
import com.example.fbi_text.database.DBHelper;

//商品详情Fragment
public class ProductDetailFragment extends Fragment {
    private ImageView productImage;
    private TextView productName;
    private TextView productPrice;
    private TextView productDescription;
    private Button addToCartBtn;
    private Button buyNowBtn;
    private ImageView cartIcon;
    private Product product;
    private DBHelper dbHelper;

    public static ProductDetailFragment newInstance(Product product) {
        ProductDetailFragment fragment = new ProductDetailFragment();
        Bundle args = new Bundle();
        args.putParcelable("product", product);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            product = getArguments().getParcelable("product");
        }
        dbHelper = DBHelper.getInstance(requireContext());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_product_detail, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        productImage = view.findViewById(R.id.detail_product_image);
        productName = view.findViewById(R.id.detail_product_name);
        productPrice = view.findViewById(R.id.detail_product_price);
        productDescription = view.findViewById(R.id.detail_product_description);
        addToCartBtn = view.findViewById(R.id.add_to_cart_btn);
        buyNowBtn = view.findViewById(R.id.buy_now_btn);
        cartIcon = view.findViewById(R.id.cart_icon);

        if (product != null) {
            productImage.setImageResource(product.getImageResId());
            productName.setText(product.getName());
            productPrice.setText(String.format("¥%s", product.getPrice()));
            productDescription.setText(product.getDescription());
        }

        addToCartBtn.setOnClickListener(v -> addToCart());
        buyNowBtn.setOnClickListener(v -> buyNow());
        cartIcon.setOnClickListener(v -> openCart());
    }

    private void addToCart() {
        if (product != null) {
            dbHelper.insertGoodsCart(
                    product.getName(),
                    product.getPrice(),
                    product.getDescription(),
                    String.valueOf(product.getReviews())
            );
            Toast.makeText(getContext(), "已加入购物车", Toast.LENGTH_SHORT).show();
        }
    }

    private void buyNow() {
        Toast.makeText(getContext(), "购买成功", Toast.LENGTH_SHORT).show();
        requireActivity().finish();
    }

    private void openCart() {
        // 这里应该跳转到购物车Activity
        Intent intent = new Intent(getActivity(), CartActivity.class);
        startActivity(intent);
        Toast.makeText(getContext(), "打开购物车", Toast.LENGTH_SHORT).show();
    }
}
